package com.hqbzl.c1.copys;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.CyclicBarrier;

@Slf4j
public class TestCopyArray {
    public static void main(String[] args) {
        final CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
        final CopyOnWriteArraySet<String> set = new CopyOnWriteArraySet<>();
        final CyclicBarrier barrier = new CyclicBarrier(3);
        new Thread(() -> {
            list.add("a");
            list.add("b");
            list.add("c");
            try {
                barrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            list.add("aa");
            list.add("bb");
            list.add("cc");
        }, "t1").start();
        new Thread(() -> {
            try {
                barrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            for (String s : list) {
                log.debug(s);
            }

        }, "t2").start();
        new Thread(() -> {
            try {
                barrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            for (String s : list) {
                log.debug(s);
            }
        }, "t3").start();
    }
}
